package com.zch.house.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zch.house.entity.FlatPlay;
import com.zch.house.service.FlatPlayService;
import com.zch.house.service.impl.FlatPlayServiceImpl;
import com.zch.house.util.DateFormat;

/**
 * Servlet implementation class FlatPlayServlet
 */
@WebServlet("/FlatPlayServlet2")
public class FlatPlayServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	FlatPlayService fps = new FlatPlayServiceImpl();
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FlatPlayServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
    /**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method = request.getParameter("method");
		if("add".equals(method)){
			add(request,response);
		}else if("delete".equals(method)){
			delete(request,response);
		}else if("update".equals(method)){
			update(request,response);
		}else if("select".equals(method)){
			select(request,response);
		}else if("gfpbi".equals(method)){
			getFlatPlayById(request,response);
		}
	}

	private void add(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String startDateStr = request.getParameter("startDate");
		Date fp_start_date = DateFormat.to_Date2(startDateStr);
		String endDateStr = request.getParameter("endDate");
		Date fp_end_date = DateFormat.to_Date2(endDateStr);
		String fp_topic = request.getParameter("topic");
		String fp_content = request.getParameter("content");
		String fp_place = request.getParameter("place");
		String moneyStr = request.getParameter("money");
		double fp_money = Double.parseDouble(moneyStr);
		String fp_organizer = request.getParameter("organizer");
		FlatPlay flatPlay = new FlatPlay(fp_start_date, fp_end_date, fp_topic, 
				fp_content, fp_place, fp_money, fp_organizer);
		boolean flag = fps.addFlatPlay(flatPlay);
		PrintWriter out = response.getWriter();
		if (flag) {
			// 添加成功,将添加成功的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('添加成功！');window.location.href='FlatPlayServlet2?method=select'</script>");
		} else {
			// 添加失败,将添加失败的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('添加失败！');window.location.href='FlatPlayServlet2?method=select'</script>");
		}
	}

	private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String idStr = request.getParameter("id");
		int id = Integer.parseInt(idStr);
		boolean flag = fps.deleteFlatPlay(id);
		// 实例化一个out对象
		PrintWriter out = response.getWriter();
		if (flag) {
			// 删除成功,将删除成功的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('删除成功！');window.location.href='FlatPlayServlet2?method=select'</script>");
		} else {
			// 删除失败,将删除失败的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('删除失败！');window.location.href='FlatPlayServlet2?method=select'</script>");
		}
	}

	private void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String idStr = request.getParameter("id");
		int id = Integer.parseInt(idStr);
		String startDateStr = request.getParameter("startDate");
		Date fp_start_date = DateFormat.to_Date2(startDateStr);
		String endDateStr = request.getParameter("endDate");
		Date fp_end_date = DateFormat.to_Date2(endDateStr);
		String fp_topic = request.getParameter("topic");
		String fp_content = request.getParameter("content");
		String fp_place = request.getParameter("place");
		String moneyStr = request.getParameter("money");
		double fp_money = Double.parseDouble(moneyStr);
		String fp_organizer = request.getParameter("organizer");
		FlatPlay flatPlay = new FlatPlay(id, fp_start_date, fp_end_date, fp_topic, 
				fp_content, fp_place, fp_money, fp_organizer);
		boolean flag = fps.updateFlatPlay(flatPlay);
		PrintWriter out = response.getWriter();
		if (flag) {
			// 修改成功,将修改成功的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('修改成功！');window.location.href='FlatPlayServlet2?method=select'</script>");
		} else {
			// 修改失败,将修改失败的信息弹框
			out.print("<head><meta charset='utf-8'></head>");// 给弹框的内容设置编码方式
			out.println(
					"<script language='javascript'>alert('修改失败！');window.location.href='FlatPlayServlet2?method=select'</script>");
		}
	}

	private void select(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String startDateStr = request.getParameter("startDate");
		Date startDate = DateFormat.to_Date2(startDateStr);
		String endDateStr = request.getParameter("endDate");
		Date endDate = DateFormat.to_Date2(endDateStr); 
		String topic = request.getParameter("topic"); 
		String pageIndexStr = request.getParameter("pageIndex");
		if(pageIndexStr==null||pageIndexStr==""){
			pageIndexStr = "1";
		}
		int pageIndex = Integer.parseInt(pageIndexStr);
		//调用service层方法
		List<FlatPlay> list = fps.getAllFlatPlayByPage(startDate, endDate, topic, pageIndex);
		//获得总页码
		int totalPages = fps.getTotalCount(startDate, endDate, topic);
		//将参数存入作用域
		request.setAttribute("startDate", startDate);
		request.setAttribute("endDate", endDate);
		request.setAttribute("topic", topic);
		request.setAttribute("pageIndex", pageIndex);
		request.setAttribute("list", list);
		request.setAttribute("totalPages", totalPages);
		request.getRequestDispatcher("view/flatplay/list.jsp").forward(request, response);
		
	}

	private void getFlatPlayById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String idStr = request.getParameter("id");
		int id = Integer.parseInt(idStr);
		FlatPlay flatPlay = fps.getFlatPlayById(id);
		request.setAttribute("flatPlay", flatPlay);
		request.getRequestDispatcher("view/flatplay/edit.jsp").forward(request, response);	
	}

}
